CREATE PROCEDURE [dbo].[asi_PostMonetaryData]
@postingData as char(12),
@batchKey uniqueidentifier = null,
@itemKey uniqueidentifier = null,
@userKey uniqueidentifier = null,
@orgKey uniqueidentifier = null,
@systemKey uniqueidentifier = null,
@accessKey uniqueidentifier = null
AS
IF EXISTS (select * from dbo.sysobjects where id = object_id('dbo.#tmpPayments'))
DROP TABLE dbo.#tmpPayments
IF EXISTS (select * from dbo.sysobjects where id = object_id('dbo.#tmpInvoice'))
DROP TABLE dbo.#tmpInvoice
CREATE TABLE #tmpInvoice
(InvoiceKey uniqueidentifier, InvoiceNumber nvarchar(50), AccountingMethodCode nchar(1),
FinancialEntityKey uniqueidentifier, DistFinancialEntityKey uniqueidentifier, PaymentTermsKey uniqueidentifier, InvoiceDate datetime, BatchKey uniqueidentifier, InvoiceLineKey uniqueidentifier, OrderLineNumber int, InvoiceLineNumber int,
PayPriority int, InvoiceDistributionKey uniqueidentifier, ExtendedIncome decimal(18,4),
ExtendedIncomeRecognized decimal(18,4), IncomeGLAccountKey uniqueidentifier,
ARGLAccountKey uniqueidentifier, DeferredIncomeGLAccountKey uniqueidentifier,
AppliedTotal decimal(18,4), InvoiceTypeCode nvarchar(50), ApplyingCredit bit, QuantitySold decimal(18,4), ParentInvoiceLineKey uniqueidentifier,
UnitIncome decimal(18,4), DeferralTermsKey uniqueidentifier, SourceCodeKey uniqueidentifier, ResponseMediaCode nvarchar(20), PriceSheetKey uniqueidentifier,
GeneratesSalesHistoryFlag bit, ExtendedCost decimal(18,4), WarehouseKey uniqueidentifier, OrderNumber nvarchar(50) COLLATE database_default, OrderTypeKey uniqueidentifier,
OrderDate datetime, BillToContactKey uniqueidentifier, ShipToContactKey uniqueidentifier, SoldToContactKey uniqueidentifier,
ProductKey uniqueidentifier, UndiscountedExtendedPrice decimal(18,4), UomKey uniqueidentifier, CommissionPlanKey uniqueidentifier, SalesTeamGroupKey uniqueidentifier, ExtendedPrice decimal(18,4),
SalesLocationKey uniqueidentifier, IsPledge bit, FirstPaymentDueDate datetime, Description nvarchar(50), OriginatingBatchNumber nvarchar(50), FinalBatchNumber nvarchar(50))
CREATE TABLE #tmpPayments
(SrcPaymentKey uniqueidentifier, SrcInvoiceLineKey uniqueidentifier, Amount decimal(18,4), FinancialEntityKey uniqueidentifier,
MonetaryApplicationKey uniqueidentifier, AppliedAmount decimal(18,4),
OrderNumber nvarchar(50) COLLATE database_default, OrderLineNumber int, InvoiceKey uniqueidentifier,
InvoiceLineKey uniqueidentifier, InvoiceDistributionKey uniqueidentifier, SrcInvoiceKey uniqueidentifier, DiscountTaken decimal(18,4),
PaymentDate datetime, InvFinancialEntityKey uniqueidentifier, BatchKey uniqueidentifier, BatchLineStatusCode int, CurrencyVariance decimal(18,4),
SalesLocationKey uniqueidentifier, Description nvarchar(50), FinalBatchNumber nvarchar(50), ContactKey uniqueidentifier,
TransactionDate datetime, TransactionType int, ApplicationBatchKey uniqueidentifier, PaymentTypePriority int,
CashGLAccountKey uniqueidentifier)
IF UPPER(@postingData) = 'PAYMENTS'
BEGIN
IF @itemKey is not null AND @itemKey != '00000000-0000-0000-0000-000000000000'
BEGIN
INSERT INTO #tmpPayments
SELECT vPostingPaymentData.SrcPaymentKey, vPostingPaymentData.SrcInvoiceLineKey, vPostingPaymentData.Amount, vPostingPaymentData.FinancialEntityKey,
vPostingPaymentData.MonetaryApplicationKey, vPostingPaymentData.AppliedAmount,
vPostingPaymentData.OrderNumber, vPostingPaymentData.OrderLineNumber, vPostingPaymentData.InvoiceKey,
vPostingPaymentData.InvoiceLineKey, vPostingPaymentData.InvoiceDistributionKey, vPostingPaymentData.SrcInvoiceKey, vPostingPaymentData.DiscountTaken,
vPostingPaymentData.PaymentDate, vPostingPaymentData.InvFinancialEntityKey, vPostingPaymentData.BatchKey, vPostingPaymentData.BatchLineStatusCode, vPostingPaymentData.CurrencyVariance,
vPostingPaymentData.SalesLocationKey, vPostingPaymentData.Description, vPostingPaymentData.FinalBatchNumber, vPostingPaymentData.ContactKey,
vPostingPaymentData.TransactionDate, vPostingPaymentData.TransactionType, vPostingPaymentData.ApplicationBatchKey,
vPostingPaymentData.PaymentTypePriority, vPostingPaymentData.CashGLAccountKey
FROM vPostingPaymentData
WHERE vPostingPaymentData.SrcPaymentKey = @itemKey
END
ELSE
BEGIN
INSERT INTO #tmpPayments
SELECT vPostingPaymentData.SrcPaymentKey, vPostingPaymentData.SrcInvoiceLineKey, vPostingPaymentData.Amount, vPostingPaymentData.FinancialEntityKey,
vPostingPaymentData.MonetaryApplicationKey, vPostingPaymentData.AppliedAmount,
vPostingPaymentData.OrderNumber, vPostingPaymentData.OrderLineNumber, vPostingPaymentData.InvoiceKey,
vPostingPaymentData.InvoiceLineKey, vPostingPaymentData.InvoiceDistributionKey, vPostingPaymentData.SrcInvoiceKey, vPostingPaymentData.DiscountTaken,
vPostingPaymentData.PaymentDate, vPostingPaymentData.InvFinancialEntityKey, vPostingPaymentData.BatchKey, vPostingPaymentData.BatchLineStatusCode, vPostingPaymentData.CurrencyVariance,
vPostingPaymentData.SalesLocationKey, vPostingPaymentData.Description, vPostingPaymentData.FinalBatchNumber, vPostingPaymentData.ContactKey,
vPostingPaymentData.TransactionDate, vPostingPaymentData.TransactionType, vPostingPaymentData.ApplicationBatchKey,
vPostingPaymentData.PaymentTypePriority, vPostingPaymentData.CashGLAccountKey
FROM vPostingPaymentData
WHERE vPostingPaymentData.BatchKey = @batchKey
END
INSERT INTO #tmpInvoice
SELECT DISTINCT PID.InvoiceKey, PID.InvoiceNumber, PID.AccountingMethodCode,
PID.FinancialEntityKey, PID.DistFinancialEntityKey, PID.PaymentTermsKey,
PID.InvoiceDate, PID.BatchKey, PID.InvoiceLineKey, PID.OrderLineNumber, PID.InvoiceLineNumber,
PID.PayPriority, PID.InvoiceDistributionKey, PID.ExtendedIncome,
PID.ExtendedIncomeRecognized, PID.IncomeGLAccountKey, PID.ARGLAccountKey,
PID.DeferredIncomeGLAccountKey, PID.AppliedTotal, PID.InvoiceTypeCode, 0, PID.QuantitySold, PID.ParentInvoiceLineKey,
PID.UnitIncome, PID.DeferralTermsKey, PID.SourceCodeKey, PID.ResponseMediaCode, PID.PriceSheetKey,
PID.GeneratesSalesHistoryFlag, PID.ExtendedCost, PID.WarehouseKey, PID.OrderNumber, PID.OrderTypeKey,
PID.OrderDate, PID.BillToContactKey, PID.ShipToContactKey, PID.SoldToContactKey,
PID.ProductKey, PID.UndiscountedExtendedPrice, PID.UomKey, PID.CommissionPlanKey, PID.SalesTeamGroupKey, PID.ExtendedPrice,
PID.SalesLocationKey, PID.IsPledge, PID.FirstPaymentDueDate,
PID.Description, PID.OriginatingBatchNumber, PID.FinalBatchNumber
FROM dbo.vPostingInvoiceData PID
INNER JOIN #tmpPayments on #tmpPayments.OrderNumber = PID.OrderNumber
WHERE #tmpPayments.InvoiceKey is null
UNION
SELECT DISTINCT PID.InvoiceKey, PID.InvoiceNumber, PID.AccountingMethodCode,
PID.FinancialEntityKey, PID.DistFinancialEntityKey, PID.PaymentTermsKey,
PID.InvoiceDate, PID.BatchKey, PID.InvoiceLineKey, PID.OrderLineNumber, PID.InvoiceLineNumber,
PID.PayPriority, PID.InvoiceDistributionKey, PID.ExtendedIncome,
PID.ExtendedIncomeRecognized, PID.IncomeGLAccountKey, PID.ARGLAccountKey,
PID.DeferredIncomeGLAccountKey, PID.AppliedTotal, PID.InvoiceTypeCode, 0, PID.QuantitySold, PID.ParentInvoiceLineKey,
PID.UnitIncome, PID.DeferralTermsKey, PID.SourceCodeKey, PID.ResponseMediaCode, PID.PriceSheetKey,
PID.GeneratesSalesHistoryFlag, PID.ExtendedCost, PID.WarehouseKey, PID.OrderNumber, PID.OrderTypeKey,
PID.OrderDate, PID.BillToContactKey, PID.ShipToContactKey, PID.SoldToContactKey,
PID.ProductKey, PID.UndiscountedExtendedPrice, PID.UomKey, PID.CommissionPlanKey, PID.SalesTeamGroupKey, PID.ExtendedPrice,
PID.SalesLocationKey, PID.IsPledge, PID.FirstPaymentDueDate,
PID.Description, PID.OriginatingBatchNumber, PID.FinalBatchNumber
FROM dbo.vPostingInvoiceData PID
INNER JOIN #tmpPayments on #tmpPayments.InvoiceKey = PID.InvoiceKey
END
ELSE IF UPPER(@postingData) = 'INVOICES'
BEGIN
IF @itemKey is not null AND @itemKey != '00000000-0000-0000-0000-000000000000'
BEGIN
INSERT INTO #tmpInvoice
SELECT DISTINCT PID.InvoiceKey, PID.InvoiceNumber, PID.AccountingMethodCode,
PID.FinancialEntityKey, PID.DistFinancialEntityKey, PID.PaymentTermsKey,
PID.InvoiceDate, PID.BatchKey, PID.InvoiceLineKey, PID.OrderLineNumber, PID.InvoiceLineNumber,
PID.PayPriority, PID.InvoiceDistributionKey, PID.ExtendedIncome,
PID.ExtendedIncomeRecognized, PID.IncomeGLAccountKey, PID.ARGLAccountKey,
PID.DeferredIncomeGLAccountKey, PID.AppliedTotal, PID.InvoiceTypeCode, 0, PID.QuantitySold, PID.ParentInvoiceLineKey,
PID.UnitIncome, PID.DeferralTermsKey, PID.SourceCodeKey, PID.ResponseMediaCode, PID.PriceSheetKey,
PID.GeneratesSalesHistoryFlag, PID.ExtendedCost, PID.WarehouseKey, PID.OrderNumber, PID.OrderTypeKey,
PID.OrderDate, PID.BillToContactKey, PID.ShipToContactKey, PID.SoldToContactKey,
PID.ProductKey, PID.UndiscountedExtendedPrice, PID.UomKey, PID.CommissionPlanKey, PID.SalesTeamGroupKey, PID.ExtendedPrice,
PID.SalesLocationKey, PID.IsPledge, PID.FirstPaymentDueDate,
PID.Description, PID.OriginatingBatchNumber, PID.FinalBatchNumber
FROM dbo.vPostingInvoiceData PID
WHERE PID.InvoiceKey = @itemKey
END
ELSE
BEGIN
INSERT INTO #tmpInvoice
SELECT DISTINCT PID.InvoiceKey, PID.InvoiceNumber, PID.AccountingMethodCode,
PID.FinancialEntityKey, PID.DistFinancialEntityKey, PID.PaymentTermsKey,
PID.InvoiceDate, PID.BatchKey, PID.InvoiceLineKey, PID.OrderLineNumber, PID.InvoiceLineNumber,
PID.PayPriority, PID.InvoiceDistributionKey, PID.ExtendedIncome,
PID.ExtendedIncomeRecognized, PID.IncomeGLAccountKey, PID.ARGLAccountKey,
PID.DeferredIncomeGLAccountKey, PID.AppliedTotal, PID.InvoiceTypeCode, 0, PID.QuantitySold, PID.ParentInvoiceLineKey,
PID.UnitIncome, PID.DeferralTermsKey, PID.SourceCodeKey, PID.ResponseMediaCode, PID.PriceSheetKey,
PID.GeneratesSalesHistoryFlag, PID.ExtendedCost, PID.WarehouseKey, PID.OrderNumber, PID.OrderTypeKey,
PID.OrderDate, PID.BillToContactKey, PID.ShipToContactKey, PID.SoldToContactKey,
PID.ProductKey, PID.UndiscountedExtendedPrice, PID.UomKey, PID.CommissionPlanKey, PID.SalesTeamGroupKey, PID.ExtendedPrice,
PID.SalesLocationKey, PID.IsPledge, PID.FirstPaymentDueDate,
PID.Description, PID.OriginatingBatchNumber, PID.FinalBatchNumber
FROM dbo.vPostingInvoiceData PID
WHERE PID.BatchKey = @batchKey
END
INSERT INTO #tmpInvoice
SELECT DISTINCT PID.InvoiceKey, PID.InvoiceNumber, PID.AccountingMethodCode,
PID.FinancialEntityKey, PID.DistFinancialEntityKey, PID.PaymentTermsKey,
PID.InvoiceDate, PID.BatchKey, PID.InvoiceLineKey, PID.OrderLineNumber, PID.InvoiceLineNumber,
PID.PayPriority, PID.InvoiceDistributionKey, PID.ExtendedIncome,
PID.ExtendedIncomeRecognized, PID.IncomeGLAccountKey, PID.ARGLAccountKey,
PID.DeferredIncomeGLAccountKey, PID.AppliedTotal, PID.InvoiceTypeCode, 1, PID.QuantitySold, PID.ParentInvoiceLineKey,
PID.UnitIncome, PID.DeferralTermsKey, PID.SourceCodeKey, PID.ResponseMediaCode, PID.PriceSheetKey,
PID.GeneratesSalesHistoryFlag, PID.ExtendedCost, PID.WarehouseKey, PID.OrderNumber, PID.OrderTypeKey,
PID.OrderDate, PID.BillToContactKey, PID.ShipToContactKey, PID.SoldToContactKey,
PID.ProductKey, PID.UndiscountedExtendedPrice, PID.UomKey, PID.CommissionPlanKey, PID.SalesTeamGroupKey, PID.ExtendedPrice,
PID.SalesLocationKey, PID.IsPledge, PID.FirstPaymentDueDate,
PID.Description, PID.OriginatingBatchNumber, PID.FinalBatchNumber
FROM dbo.vPostingInvoiceData PID
INNER JOIN MonetaryApplication ma ON PID.InvoiceKey = ma.InvoiceKey
INNER JOIN #tmpInvoice ti on ti.InvoiceLineKey = ma.SrcInvoiceLineKey
INSERT INTO #tmpPayments
SELECT PPD.SrcPaymentKey, PPD.SrcInvoiceLineKey, PPD.Amount, PPD.FinancialEntityKey,
PPD.MonetaryApplicationKey, PPD.AppliedAmount,
PPD.OrderNumber, PPD.OrderLineNumber, PPD.InvoiceKey,
PPD.InvoiceLineKey, PPD.InvoiceDistributionKey, PPD.SrcInvoiceKey,
PPD.DiscountTaken, PPD.PaymentDate, PPD.InvFinancialEntityKey, PPD.BatchKey, PPD.BatchLineStatusCode, PPD.CurrencyVariance,
PPD.SalesLocationKey, PPD.Description, PPD.FinalBatchNumber, PPD.ContactKey, PPD.TransactionDate, PPD.TransactionType,
PPD.ApplicationBatchKey, PPD.PaymentTypePriority, PPD.CashGLAccountKey
FROM vPostingPaymentData PPD
INNER JOIN #tmpInvoice on #tmpInvoice.OrderNumber = PPD.OrderNumber
WHERE PPD.InvoiceKey is null
UNION
SELECT PPD.SrcPaymentKey, PPD.SrcInvoiceLineKey, PPD.Amount, PPD.FinancialEntityKey,
PPD.MonetaryApplicationKey, PPD.AppliedAmount,
PPD.OrderNumber, PPD.OrderLineNumber, PPD.InvoiceKey,
PPD.InvoiceLineKey, PPD.InvoiceDistributionKey, PPD.SrcInvoiceKey,
PPD.DiscountTaken, PPD.PaymentDate, PPD.InvFinancialEntityKey, PPD.BatchKey, PPD.BatchLineStatusCode, PPD.CurrencyVariance,
PPD.SalesLocationKey, PPD.Description, PPD.FinalBatchNumber, PPD.ContactKey, PPD.TransactionDate, PPD.TransactionType,
PPD.ApplicationBatchKey, PPD.PaymentTypePriority, PPD.CashGLAccountKey
FROM dbo.vPostingPaymentData PPD
INNER JOIN #tmpInvoice on #tmpInvoice.InvoiceKey = PPD.InvoiceKey
WHERE PPD.SrcInvoiceLineKey is not null OR #tmpInvoice.ApplyingCredit = 0
EXEC asi_CreatePaymentSchedules
END
DECLARE @creditInvDistCreated bit
EXEC asi_ReApplyPayments @creditInvDistCreated output
IF UPPER(@postingData) = 'INVOICES' AND @creditInvDistCreated = 1
BEGIN
IF @itemKey is not null AND @itemKey != '00000000-0000-0000-0000-000000000000'
BEGIN
INSERT INTO #tmpInvoice
SELECT DISTINCT PID.InvoiceKey, PID.InvoiceNumber, PID.AccountingMethodCode,
PID.FinancialEntityKey, PID.DistFinancialEntityKey, PID.PaymentTermsKey,
PID.InvoiceDate, PID.BatchKey, PID.InvoiceLineKey, PID.OrderLineNumber, PID.InvoiceLineNumber,
PID.PayPriority, PID.InvoiceDistributionKey, PID.ExtendedIncome,
PID.ExtendedIncomeRecognized, PID.IncomeGLAccountKey, PID.ARGLAccountKey,
PID.DeferredIncomeGLAccountKey, PID.AppliedTotal, PID.InvoiceTypeCode, 0, PID.QuantitySold, PID.ParentInvoiceLineKey,
PID.UnitIncome, PID.DeferralTermsKey, PID.SourceCodeKey, PID.ResponseMediaCode, PID.PriceSheetKey,
PID.GeneratesSalesHistoryFlag, PID.ExtendedCost, PID.WarehouseKey, PID.OrderNumber, PID.OrderTypeKey,
PID.OrderDate, PID.BillToContactKey, PID.ShipToContactKey, PID.SoldToContactKey,
PID.ProductKey, PID.UndiscountedExtendedPrice, PID.UomKey, PID.CommissionPlanKey, PID.SalesTeamGroupKey, PID.ExtendedPrice,
PID.SalesLocationKey, PID.IsPledge, PID.FirstPaymentDueDate,
PID.Description, PID.OriginatingBatchNumber, PID.FinalBatchNumber
FROM dbo.vPostingInvoiceData PID
WHERE PID.InvoiceKey = @itemKey
END
ELSE
BEGIN
INSERT INTO #tmpInvoice
SELECT DISTINCT PID.InvoiceKey, PID.InvoiceNumber, PID.AccountingMethodCode,
PID.FinancialEntityKey, PID.DistFinancialEntityKey, PID.PaymentTermsKey,
PID.InvoiceDate, PID.BatchKey, PID.InvoiceLineKey, PID.OrderLineNumber, PID.InvoiceLineNumber,
PID.PayPriority, PID.InvoiceDistributionKey, PID.ExtendedIncome,
PID.ExtendedIncomeRecognized, PID.IncomeGLAccountKey, PID.ARGLAccountKey,
PID.DeferredIncomeGLAccountKey, PID.AppliedTotal, PID.InvoiceTypeCode, 0, PID.QuantitySold, PID.ParentInvoiceLineKey,
PID.UnitIncome, PID.DeferralTermsKey, PID.SourceCodeKey, PID.ResponseMediaCode, PID.PriceSheetKey,
PID.GeneratesSalesHistoryFlag, PID.ExtendedCost, PID.WarehouseKey, PID.OrderNumber, PID.OrderTypeKey,
PID.OrderDate, PID.BillToContactKey, PID.ShipToContactKey, PID.SoldToContactKey,
PID.ProductKey, PID.UndiscountedExtendedPrice, PID.UomKey, PID.CommissionPlanKey, PID.SalesTeamGroupKey, PID.ExtendedPrice,
PID.SalesLocationKey, PID.IsPledge, PID.FirstPaymentDueDate,
PID.Description, PID.OriginatingBatchNumber, PID.FinalBatchNumber
FROM dbo.vPostingInvoiceData PID
WHERE PID.BatchKey = @batchKey
END
INSERT INTO #tmpInvoice
SELECT DISTINCT PID.InvoiceKey, PID.InvoiceNumber, PID.AccountingMethodCode,
PID.FinancialEntityKey, PID.DistFinancialEntityKey, PID.PaymentTermsKey,
PID.InvoiceDate, PID.BatchKey, PID.InvoiceLineKey, PID.OrderLineNumber, PID.InvoiceLineNumber,
PID.PayPriority, PID.InvoiceDistributionKey, PID.ExtendedIncome,
PID.ExtendedIncomeRecognized, PID.IncomeGLAccountKey, PID.ARGLAccountKey,
PID.DeferredIncomeGLAccountKey, PID.AppliedTotal, PID.InvoiceTypeCode, 1, PID.QuantitySold, PID.ParentInvoiceLineKey,
PID.UnitIncome, PID.DeferralTermsKey, PID.SourceCodeKey, PID.ResponseMediaCode, PID.PriceSheetKey,
PID.GeneratesSalesHistoryFlag, PID.ExtendedCost, PID.WarehouseKey, PID.OrderNumber, PID.OrderTypeKey,
PID.OrderDate, PID.BillToContactKey, PID.ShipToContactKey, PID.SoldToContactKey,
PID.ProductKey, PID.UndiscountedExtendedPrice, PID.UomKey, PID.CommissionPlanKey, PID.SalesTeamGroupKey, PID.ExtendedPrice,
PID.SalesLocationKey, PID.IsPledge, PID.FirstPaymentDueDate,
PID.Description, PID.OriginatingBatchNumber, PID.FinalBatchNumber
FROM dbo.vPostingInvoiceData PID
INNER JOIN MonetaryApplication ma ON PID.InvoiceKey = ma.InvoiceKey
INNER JOIN #tmpInvoice ti on ti.InvoiceLineKey = ma.SrcInvoiceLineKey
END
DELETE #tmpPayments
IF UPPER(@postingData) = 'PAYMENTS'
BEGIN
IF @itemKey is not null AND @itemKey != '00000000-0000-0000-0000-000000000000'
BEGIN
INSERT INTO #tmpPayments
SELECT vPostingPaymentData.SrcPaymentKey, vPostingPaymentData.SrcInvoiceLineKey, vPostingPaymentData.Amount, vPostingPaymentData.FinancialEntityKey,
vPostingPaymentData.MonetaryApplicationKey, vPostingPaymentData.AppliedAmount,
vPostingPaymentData.OrderNumber, vPostingPaymentData.OrderLineNumber, vPostingPaymentData.InvoiceKey,
vPostingPaymentData.InvoiceLineKey, vPostingPaymentData.InvoiceDistributionKey, vPostingPaymentData.SrcInvoiceKey, vPostingPaymentData.DiscountTaken,
vPostingPaymentData.PaymentDate, vPostingPaymentData.InvFinancialEntityKey, vPostingPaymentData.BatchKey, vPostingPaymentData.BatchLineStatusCode, vPostingPaymentData.CurrencyVariance,
vPostingPaymentData.SalesLocationKey, vPostingPaymentData.Description, vPostingPaymentData.FinalBatchNumber, vPostingPaymentData.ContactKey,
vPostingPaymentData.TransactionDate, vPostingPaymentData.TransactionType, vPostingPaymentData.ApplicationBatchKey,
vPostingPaymentData.PaymentTypePriority, vPostingPaymentData.CashGLAccountKey
FROM vPostingPaymentData
WHERE vPostingPaymentData.SrcPaymentKey = @itemKey
END
ELSE
BEGIN
INSERT INTO #tmpPayments
SELECT vPostingPaymentData.SrcPaymentKey, vPostingPaymentData.SrcInvoiceLineKey, vPostingPaymentData.Amount, vPostingPaymentData.FinancialEntityKey,
vPostingPaymentData.MonetaryApplicationKey, vPostingPaymentData.AppliedAmount,
vPostingPaymentData.OrderNumber, vPostingPaymentData.OrderLineNumber, vPostingPaymentData.InvoiceKey,
vPostingPaymentData.InvoiceLineKey, vPostingPaymentData.InvoiceDistributionKey, vPostingPaymentData.SrcInvoiceKey, vPostingPaymentData.DiscountTaken,
vPostingPaymentData.PaymentDate, vPostingPaymentData.InvFinancialEntityKey, vPostingPaymentData.BatchKey, vPostingPaymentData.BatchLineStatusCode, vPostingPaymentData.CurrencyVariance,
vPostingPaymentData.SalesLocationKey, vPostingPaymentData.Description, vPostingPaymentData.FinalBatchNumber, vPostingPaymentData.ContactKey,
vPostingPaymentData.TransactionDate, vPostingPaymentData.TransactionType, vPostingPaymentData.ApplicationBatchKey,
vPostingPaymentData.PaymentTypePriority, vPostingPaymentData.CashGLAccountKey
FROM vPostingPaymentData
WHERE vPostingPaymentData.BatchKey = @batchKey
END
END
ELSE IF UPPER(@postingData) = 'INVOICES'
BEGIN
INSERT INTO #tmpPayments
SELECT PPD.SrcPaymentKey, PPD.SrcInvoiceLineKey, PPD.Amount, PPD.FinancialEntityKey,
PPD.MonetaryApplicationKey, PPD.AppliedAmount,
PPD.OrderNumber, PPD.OrderLineNumber, PPD.InvoiceKey,
PPD.InvoiceLineKey, PPD.InvoiceDistributionKey, PPD.SrcInvoiceKey,
PPD.DiscountTaken, PPD.PaymentDate, PPD.InvFinancialEntityKey, PPD.BatchKey, PPD.BatchLineStatusCode, PPD.CurrencyVariance,
PPD.SalesLocationKey, PPD.Description, PPD.FinalBatchNumber, PPD.ContactKey, PPD.TransactionDate, PPD.TransactionType,
PPD.ApplicationBatchKey, PPD.PaymentTypePriority, PPD.CashGLAccountKey
FROM vPostingPaymentData PPD
INNER JOIN #tmpInvoice on #tmpInvoice.InvoiceDistributionKey = PPD.InvoiceDistributionKey
END
EXEC asi_PostGLData @postingData, @userKey, @orgKey, @systemKey, @accessKey
DROP TABLE #tmpInvoice
DROP TABLE #tmpPayments
IF @batchKey is not null AND @batchKey != '00000000-0000-0000-0000-000000000000'
BEGIN
UPDATE InvoiceMain set BatchLineStatusCode = 1
WHERE FinalBatchKey = @batchKey
UPDATE PaymentMain set BatchLineStatusCode = 1
WHERE FinalBatchKey = @batchKey
END
GO